package com.gzc.just.play.last.war.sceneserver.entity;

import com.fasterxml.jackson.annotation.JsonIgnore;
import jakarta.persistence.*;
import org.springframework.data.annotation.CreatedDate;
import org.springframework.data.annotation.LastModifiedDate;
import org.springframework.data.jpa.domain.support.AuditingEntityListener;

import java.time.LocalDateTime;

/**
 * 玩家属性实体类
 * 
 * 对应数据库表：player_attributes
 * 作为Phase 4.3数据库持久化集成的一部分
 */
@Entity
@Table(name = "player_attributes", 
       indexes = {
           @Index(name = "idx_attribute_player_id", columnList = "player_id"),
           @Index(name = "idx_attribute_type", columnList = "attribute_type")
       },
       uniqueConstraints = {
           @UniqueConstraint(name = "uk_player_attribute", columnNames = {"player_id", "attribute_type"})
       })
@EntityListeners(AuditingEntityListener.class)
public class PlayerAttribute {
    
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private Long id;
    
    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn(name = "player_id", nullable = false, foreignKey = @ForeignKey(name = "fk_player_attribute_player"))
    @JsonIgnore
    private Player player;
    
    @Column(name = "attribute_type", nullable = false)
    private Integer attributeType;
    
    @Column(name = "base_value", nullable = false)
    private Integer baseValue;
    
    @Column(name = "current_value", nullable = false)
    private Integer currentValue;
    
    @CreatedDate
    @Column(name = "created_at", nullable = false, updatable = false)
    private LocalDateTime createdAt;
    
    @LastModifiedDate
    @Column(name = "updated_at", nullable = false)
    private LocalDateTime updatedAt;
    
    // 属性类型常量
    public static final int ATTRIBUTE_TYPE_ATTACK = 1;      // 攻击力
    public static final int ATTRIBUTE_TYPE_DEFENSE = 2;     // 防御力
    public static final int ATTRIBUTE_TYPE_HEALTH = 3;      // 生命值
    public static final int ATTRIBUTE_TYPE_MAX_HEALTH = 4;   // 最大生命值
    public static final int ATTRIBUTE_TYPE_MANA = 5;        // 法力值
    public static final int ATTRIBUTE_TYPE_MAX_MANA = 6;    // 最大法力值
    public static final int ATTRIBUTE_TYPE_CRITICAL = 7;    // 暴击率
    public static final int ATTRIBUTE_TYPE_DODGE = 8;       // 闪避率
    public static final int ATTRIBUTE_TYPE_SPEED = 9;       // 移动速度
    public static final int ATTRIBUTE_TYPE_ATTACK_SPEED = 10; // 攻击速度
    
    // 构造函数
    public PlayerAttribute() {}
    
    public PlayerAttribute(Player player, Integer attributeType, Integer baseValue) {
        this.player = player;
        this.attributeType = attributeType;
        this.baseValue = baseValue;
        this.currentValue = baseValue;
    }
    
    // Getter和Setter方法
    public Long getId() {
        return id;
    }
    
    public void setId(Long id) {
        this.id = id;
    }
    
    public Player getPlayer() {
        return player;
    }
    
    public void setPlayer(Player player) {
        this.player = player;
    }
    
    public Integer getAttributeType() {
        return attributeType;
    }
    
    public void setAttributeType(Integer attributeType) {
        this.attributeType = attributeType;
    }
    
    public Integer getBaseValue() {
        return baseValue;
    }
    
    public void setBaseValue(Integer baseValue) {
        this.baseValue = baseValue;
    }
    
    public Integer getCurrentValue() {
        return currentValue;
    }
    
    public void setCurrentValue(Integer currentValue) {
        this.currentValue = currentValue;
    }
    
    public LocalDateTime getCreatedAt() {
        return createdAt;
    }
    
    public void setCreatedAt(LocalDateTime createdAt) {
        this.createdAt = createdAt;
    }
    
    public LocalDateTime getUpdatedAt() {
        return updatedAt;
    }
    
    public void setUpdatedAt(LocalDateTime updatedAt) {
        this.updatedAt = updatedAt;
    }
    
    // 业务方法
    public void adjustCurrentValue(int adjustment) {
        this.currentValue += adjustment;
    }
    
    public void resetToBase() {
        this.currentValue = this.baseValue;
    }
    
    public void setBaseValue(int newBaseValue) {
        this.baseValue = newBaseValue;
        if (this.currentValue < this.baseValue) {
            this.currentValue = this.baseValue;
        }
    }
    
    public String getAttributeName() {
        switch (attributeType) {
            case ATTRIBUTE_TYPE_ATTACK:
                return "攻击力";
            case ATTRIBUTE_TYPE_DEFENSE:
                return "防御力";
            case ATTRIBUTE_TYPE_HEALTH:
                return "生命值";
            case ATTRIBUTE_TYPE_MAX_HEALTH:
                return "最大生命值";
            case ATTRIBUTE_TYPE_MANA:
                return "法力值";
            case ATTRIBUTE_TYPE_MAX_MANA:
                return "最大法力值";
            case ATTRIBUTE_TYPE_CRITICAL:
                return "暴击率";
            case ATTRIBUTE_TYPE_DODGE:
                return "闪避率";
            case ATTRIBUTE_TYPE_SPEED:
                return "移动速度";
            case ATTRIBUTE_TYPE_ATTACK_SPEED:
                return "攻击速度";
            default:
                return "未知属性";
        }
    }
    
    @Override
    public String toString() {
        return "PlayerAttribute{" +
                "id=" + id +
                ", player=" + (player != null ? player.getPlayerId() : null) +
                ", attributeType=" + attributeType +
                ", attributeName='" + getAttributeName() + '\'' +
                ", baseValue=" + baseValue +
                ", currentValue=" + currentValue +
                ", createdAt=" + createdAt +
                '}';
    }
    
    @Override
    public boolean equals(Object o) {
        if (this == o) return true;
        if (o == null || getClass() != o.getClass()) return false;
        
        PlayerAttribute that = (PlayerAttribute) o;
        
        if (id != null ? id.equals(that.id) : that.id == null) return false;
        if (player != null ? !player.equals(that.player) : that.player != null) return false;
        return attributeType != null ? attributeType.equals(that.attributeType) : that.attributeType == null;
    }
    
    @Override
    public int hashCode() {
        int result = id != null ? id.hashCode() : 0;
        result = 31 * result + (player != null ? player.hashCode() : 0);
        result = 31 * result + (attributeType != null ? attributeType.hashCode() : 0);
        return result;
    }
}